﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using SangoBang;

namespace TestProject1
{
    /// <summary>
    /// Summary description for GetCard
    /// </summary>
    [TestClass]
    public class GetCard
    {
        public GetCard()
        {
            mEventController = new EventController();

            string msg;
            for (int i = 0; i < mPlayerNum; ++i)
            {
                msg = "<message type=\"newuser\"><user>" + mUsernames[i] + "</user></message>";
                string guid = mEventController.OnReceiveMsg(msg, "");
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(guid);
                XmlElement replyMsgElement = doc.DocumentElement;
                mGuids[i] = replyMsgElement.FirstChild.InnerText;
            }
        }
        private string[] mUsernames={"player1","player2","player3","player4","player5","player6","player7","player8","player9"};
        private string[] mGuids = new string[9];
        private string[] mRoles = new string[9];
        private EventController mEventController;
        private TestContext testContextInstance;
        private int mPlayerNum = 4;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext) 
        //{
            
        //}
        
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void GetCard1()
        {
            string msg;
            XmlDocument doc = new XmlDocument();
                       
            // player 1 发出开始游戏消息
            msg = "<message type=\"startGame\"></message>";
            mEventController.OnReceiveMsg(msg, mGuids[0]);
            // 所有玩家收到身份牌
            int zhugongIndex = 0;
            for (int i = 0; i < mPlayerNum; ++i)
            {
                XmlElement msgElement;
                string type;
                int max = mPlayerNum * 2 + 2;
                int count = 0;
                do { 
                    msg = mEventController.GetMsg(mGuids[i]);
                    //message sample
                    //msg = "<message type=\"roleCard\"><role>zhugong</role></message>"; 
                    System.Console.WriteLine(msg);
                    doc.LoadXml(msg);
                    msgElement = doc.DocumentElement;
                    type = msgElement.GetAttribute("type");
                    count++; 
                } while (type != "roleCard" && type!="empty" && count < max );
                Assert.AreEqual(type, "roleCard", "should got roleCard, actual type is "+ type);
                string role = msgElement.FirstChild.InnerText;
                mRoles[i] = role;
                if (role == "P Zhugong") 
                {
                    zhugongIndex = i;
                }
            }
            // 所有人收到消息，谁是 Zhugong            
            for (int i = 0; i < mPlayerNum; ++i)
            {
                XmlElement msgElement;
                string type;
                int max = mPlayerNum * 2 + 2;
                int count = 0;
                if (mRoles[i] == "P Zhugong")
                { 
                    continue;
                }
                do
                {
                    msg = mEventController.GetMsg(mGuids[i]);
                    //message sample
                    //msg = "<message type=\"playerRole\"><role player=\"player1\">zhugong</role></message>";
                    doc.LoadXml(msg);
                    msgElement = doc.DocumentElement;
                    type = msgElement.GetAttribute("type");
                    count++;
                } while (type != "playerRole" && type != "empty" && count < max);
                Assert.AreEqual(type, "playerRole", "should got playerRole, actual type is " + type);
                string role = msgElement.FirstChild.InnerText;
                Assert.AreEqual(role, "P Zhugong", "role should be zhugong");
                Assert.AreEqual(mUsernames[zhugongIndex], ((XmlElement)msgElement.FirstChild).GetAttribute("player"), 
                    "zhugong's player name should be correct");
            }
            // 所有人收到角色牌,包括其他人的角色
            for (int i = 0; i < mPlayerNum; ++i)
            {
                XmlElement msgElement;
                string type;
                int max = mPlayerNum * 2 + 2;
                int count = 0;
                do
                {
                    msg = mEventController.GetMsg(mGuids[i]);
                    //message sample
                    //msg = "<message type=\"characterCard\">" +
                    //    "<character player=\"player1\">liubei</character>" +
                    //    "<character player=\"player2\">zhangfei</character>" +
                    //    "<character player=\"player3\">zhaoyun</character>" +
                    //    "<character player=\"player4\">guanyu</character>" +
                    //    "</message>";
                     
                    doc.LoadXml(msg);
                    msgElement = doc.DocumentElement;
                    type = msgElement.GetAttribute("type");
                    count++;
                } while (type != "characterCard" && type != "empty" && count < max);
                Assert.AreEqual(type, "characterCard", "should got characterCard, actual type is " + type);
                foreach (XmlElement character in msgElement){
                    Assert.AreEqual("character", character.Name,"should only contain character element");
                    string playername = character.GetAttribute("player");
                    string HPtext = character.GetAttribute("hp");
                    string description = character.GetAttribute("description");
                    CollectionAssert.Contains(mUsernames, playername);
                }
                
            }
            // 所有人收到游戏牌,数量等于4
            for (int i = 0; i < mPlayerNum; ++i)
            {
                XmlElement msgElement;
                string type;
                int max = mPlayerNum * 2 + 2;
                int count = 0;
                do
                {
                    msg = mEventController.GetMsg(mGuids[i]);
                    //message sample
                    //msg = "<message type=\"gameCard\">" +
                    //    "<gameCard>[游戏牌]</gameCard>" +
                    //    "<gameCard>[游戏牌]</gameCard>" +
                    //    "<gameCard>[游戏牌]</gameCard>" +
                    //    "<gameCard>[游戏牌]</gameCard>" +
                    //    "</message>";

                    doc.LoadXml(msg);
                    msgElement = doc.DocumentElement;
                    type = msgElement.GetAttribute("type");
                    count++;
                } while (type != "gameCard" && type != "empty" && count < max);
                Assert.AreEqual(type, "gameCard", "should got gameCard");
                Assert.AreEqual(4, msgElement.GetElementsByTagName("gameCard").Count, "should get 4 game card");
                foreach (XmlElement gameCard in msgElement)
                {
                    Assert.AreEqual("gameCard", gameCard.Name, "should only contain gameCard element");
                }
            }
            //得到发牌结束的消息
            for (int i = 0; i < mPlayerNum; ++i)
            {
                XmlElement msgElement;
                string type;
                int max = mPlayerNum * 2 + 2;
                int count = 0;
                do
                {
                    msg = mEventController.GetMsg(mGuids[i]);
                    //message sample
                    //msg = "<message type=\"dispatchCardFinish\"/>";

                    doc.LoadXml(msg);
                    msgElement = doc.DocumentElement;
                    type = msgElement.GetAttribute("type");
                    count++;
                } while (type != "dispatchCardFinish" && type != "empty" && count < max);
                Assert.AreEqual(type, "dispatchCardFinish", "should got dispatchCardFinish");

            }
        }
    }
}
